/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.sofa.shark.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sofa.shark.admin.entity.SysFiles;
import com.sofa.shark.admin.service.SysFilesServices;
import com.sofa.shark.common.core.constant.CommonConstants;
import com.sofa.shark.common.core.exception.BusinessException;
import com.sofa.shark.common.core.utils.R;
import com.sofa.shark.common.log.apm.annotation.SysLog;
import com.sofa.shark.file.enums.ExceptionEnums;
import com.sofa.shark.file.enums.FileUploadSceneType;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * 系统文件表
 *
 * @author tonypy
 * @date 2020-04-01 14:48:16
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sysfiles")
public class SysFilesController {

    private final SysFilesServices sysFilesService;

    /**
     * 分页查询
     *
     * @param page     分页对象
     * @param sysFiles 系统文件表
     * @return
     */
    @GetMapping("/page")
    public R getSysFilesPage(Page page, SysFiles sysFiles) {
        return R.ok(sysFilesService.page(page, Wrappers.query(sysFiles)));
    }


    /**
     * 通过id查询系统文件表
     *
     * @param id id
     * @return R
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") String id) {
        return R.ok(sysFilesService.getById(id));
    }

    /**
     * 新增系统文件表
     *
     * @param sysFiles 系统文件表
     * @return R
     */
    @SysLog("新增系统文件表")
    @PostMapping
    public R save(@RequestBody SysFiles sysFiles) {
        return R.ok(sysFilesService.save(sysFiles));
    }

    /**
     * 修改系统文件表
     *
     * @param sysFiles 系统文件表
     * @return R
     */
    @SysLog("修改系统文件表")
    @PutMapping
    public R updateById(@RequestBody SysFiles sysFiles) {
        return R.ok(sysFilesService.updateById(sysFiles));
    }

    /**
     * 通过id删除系统文件表
     *
     * @param id id
     * @return R
     */
    @SysLog("通过id删除系统文件表")
    @DeleteMapping("/{id}")
    public R removeById(@PathVariable String id) {
        return R.ok(sysFilesService.removeById(id));
    }


    /**
     * 上传附件信息
     *
     * @param file 文件
     * @return R
     */
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    @ResponseBody
    @SysLog("上传附件信息")
    public R uploadFile(@RequestParam MultipartFile file,
                        String type) {
        if (file.getSize() >= CommonConstants.MAX_UPLOAD_IMAGE_SIZE) {
            throw new BusinessException(ExceptionEnums.IMAGE_OVER_8M.getName());
        }
        if (StringUtils.isBlank(type)) {
            type = FileUploadSceneType.DEFAULT.name();
        }
        SysFiles sysFiles = sysFilesService.saveFiles(file, FileUploadSceneType.valueOf(type));
        return R.ok(sysFiles);
    }
}
